package cn.com.bluemoon.daps.system.controller;


import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.system.dto.SysRoleDto;
import cn.com.bluemoon.daps.system.dto.SysPageDto;
import cn.com.bluemoon.daps.system.entity.DapSysRole;
import cn.com.bluemoon.daps.system.service.DapSysRoleService;
import cn.com.bluemoon.daps.system.vo.DapSysRoleVo;
import cn.com.bluemoon.daps.system.vo.SysRoleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-12-19
 */
@Api(tags = {"系统角色管理"})
@RestController
@Slf4j
@RequestMapping("/dapSysRole")
public class DapSysRoleController {

    @Autowired
    private DapSysRoleService sysRoleService;

    @ApiOperation("角色列表")
    @PostMapping(value = "/role/page")
    @BmPermission(value = "dap:system:role:page",ignore = true)
    public ResultBean<IPage<DapSysRole>> page(@RequestBody SysPageDto pageDto){
        IPage<DapSysRole> userPage = sysRoleService.rolePage(pageDto);
        return ResultBean.ok(userPage);
    }

    @ApiOperation("根据ID获取角色")
    @GetMapping(value = "/role/getRoleById")
    @BmPermission(value = "dap:system:role:getRoleById",ignore = true)
    public ResultBean<SysRoleVo> getRoleById(@RequestParam("id") Long id){
        return ResultBean.ok(sysRoleService.getRoleById(id));
    }

    @ApiOperation("新增角色")
    @PostMapping(value = "/role/add")
    @BmPermission(value = "dap:system:role:add",ignore = true)
    public ResultBean add(@RequestBody SysRoleDto dto){
        if(Objects.nonNull(dto.getId())){
            dto.setId(null);
        }
        return ResultBean.ok(sysRoleService.saveRole(dto));
    }

    @ApiOperation("修改角色")
    @PostMapping(value = "/role/update")
    @BmPermission(value = "dap:system:role:update",ignore = true)
    public ResultBean update(@RequestBody SysRoleDto dto){
        if(Objects.isNull(dto.getId())){
            return ResultBean.error(String.format("角色 %s 的ID不能为空", dto.getName()));
        }
        return ResultBean.ok(sysRoleService.saveRole(dto));
    }

    @ApiOperation("删除角色")
    @DeleteMapping(value = "/role/delete")
    @BmPermission(value = "dap:system:role:delete",ignore = true)
    public ResultBean<Boolean> delete(@RequestParam("id") Long id){
        return ResultBean.ok(sysRoleService.deleteRole(id));
    }

    @ApiOperation("获取启用状态的角色下拉列表")
    @GetMapping(value = "/role/getRoleList")
    @BmPermission(value = "dap:system:role:getRoleList",ignore = true)
    public ResultBean<List<DapSysRoleVo>> getRoleList(){
        return ResultBean.ok(sysRoleService.getRoleVoList());
    }
}
